﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 战斗角色
/// </summary>
public class FightPersonage : MonoBehaviour {

    /// <summary>
    /// 队伍序数（-1表示敌人，1表示友方）
    /// </summary>
    public int TeamIndex = -1;
    /// <summary>
    /// 队伍中的人员序数（0中间，1左边，2右边）
    /// </summary>
    public int PersonageIndex;
    /// <summary>
    /// 人物id
    /// </summary>
    public string PersonageId { get; set; }
    public NumberTips NumberTips;

    /// <summary>
    /// 人物站立位置
    /// </summary>
    public Vector3 StandPosition { get; set; }
    /// <summary>
    /// 人物站立方向
    /// </summary>
    protected Quaternion StandRotation;
    /// <summary>
    /// 对应人物数据
    /// </summary>
    protected Personage ThePersonage;
    /// <summary>
    /// 刚体
    /// </summary>
    protected Rigidbody Rgd;
    /// <summary>
    /// 进攻标志（0：待命；1：出发；2：攻击；3：返回；4：结束）
    /// </summary>
    public int Flag = 0;
    /// <summary>
    /// 目标人物
    /// </summary>
    protected FightPersonage TargetPerson;
    /// <summary>
    /// 移动速度
    /// </summary>
    protected float MoveSpeed = 10;
    /// <summary>
    /// 旋转速度
    /// </summary>
    private float RotateSpeed = 20;
    /// <summary>
    /// 战斗距离
    /// </summary>
    private float Offset = 1.1f;
    private Vector3 EscapeVector = new Vector3(10, 0, 0);

    // Use this for initialization
    void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
        MoveUpdate();
    }

    public void Init() {
        GlobalData globalData = GlobalData.Instance;
        if (this.TeamIndex == -1 && globalData.EnemyFightTeam.Count > this.PersonageIndex) { // 如果是敌人 && 敌人队伍人数大于人物序数
            this.ThePersonage = globalData.EnemyFightTeam[this.PersonageIndex];
            this.PersonageId = this.ThePersonage.PersonageId;
        } else if (this.TeamIndex == 1 && globalData.OurFightTeam.Count > this.PersonageIndex) { // 如果是友方 && 友方队伍人数大于人物序数
            this.ThePersonage = globalData.OurFightTeam[this.PersonageIndex];
            this.PersonageId = this.ThePersonage.PersonageId;
        } else { // 其他情况隐藏
            this.gameObject.SetActive(false);
        }
        
        this.StandPosition = transform.position;
        this.StandRotation = transform.rotation;
        this.Rgd = GetComponent<Rigidbody>();
    }

    protected void MoveUpdate() {
        float offset = this.Offset * this.TeamIndex;
        if (Flag == 1) { // 攻击状态
            Vector3 destination = TargetPerson.StandPosition + new Vector3(offset, 0, 0);
            //Rgd.MovePosition(Vector3.SmoothDamp(Rgd.position, destination, ref Velocity, MoveSpeed * Time.deltaTime));
            Rgd.MovePosition(Vector3.MoveTowards(Rgd.position, destination, MoveSpeed * Time.deltaTime));
            if (Vector3.Distance(Rgd.position, destination) < 0.01f) {
                Rgd.position = destination;
                Flag = 2; // 攻击中
                StartCoroutine(ComeBack());
            }
        } else if (Flag == 3) { // 返回状态
            Rgd.MovePosition(Vector3.MoveTowards(Rgd.position, StandPosition, MoveSpeed * Time.deltaTime));
            //Rgd.MovePosition(Vector3.SmoothDamp(Rgd.position, StandPosition, ref Velocity, MoveSpeed * Time.deltaTime));
            if (Vector3.Distance(Rgd.position, StandPosition) < 0.01f) {
                Rgd.position = StandPosition;
                Flag = 0; // 回到初始位置
                FightManager.Instance.PersonageStartAction();
            }
        } else if (Flag == 4) { // 御毒
            Rgd.AddForce(Vector3.up * 100);
            Flag = 0;
            FightManager.Instance.Invoke("PersonageStartAction", 2);
        } else if (Flag == 5) { // 使用物品

        } else if (Flag == 6) { // 逃跑
            Quaternion target = Quaternion.LookRotation(this.EscapeVector);
            Rgd.MoveRotation(Quaternion.Lerp(Rgd.rotation, target, RotateSpeed * Time.deltaTime));
            Vector3 destination = this.StandPosition + this.EscapeVector;
            Rgd.MovePosition(Vector3.MoveTowards(Rgd.position, destination, MoveSpeed * Time.deltaTime));
            if (Vector3.Distance(Rgd.position, destination) < 0.01f) {
                Rgd.position = destination;
                Flag = 7; // 已逃跑
                FightManager.Instance.EscapeSuccess();
            }
        } else if (Flag == 8) { // 休息
            Rgd.AddForce(Vector3.up * 100);
            List<int> nums = this.ThePersonage.Rest();
            this.NumberTips.ShowHpMpVit(nums[0], nums[1], nums[2], this.gameObject);
            Flag = 0;
            FightManager.Instance.Invoke("PersonageStartAction", 2);
        }
    }

    /// <summary>
    /// 攻击
    /// </summary>
    /// <param name="person"></param>
    public void Attack(FightPersonage person) {
        TargetPerson = person;
        Flag = 1;
    }

    /// <summary>
    /// 御毒
    /// </summary>
    public void ResistPoison() {
        Flag = 4;
    }

    /// <summary>
    /// 使用物品
    /// </summary>
    public void UseArticle() {
        Flag = 5;
    }

    /// <summary>
    /// 休息
    /// </summary>
    public void Rest() {
        Flag = 8;
    }

    /// <summary>
    /// 逃跑
    /// </summary>
    public void Escape() {
        Flag = 6;
    }

    public IEnumerator ComeBack() {
        yield return new WaitForSeconds(0.6f);
        Flag = 3;
        List<int> damages = ThePersonage.Attack();
        this.NumberTips.ShowHp(damages[0], this.TargetPerson.gameObject);
    }
}
